#include <QPainter>
#include <QMenu>
#include <QAction>
#include <QDebug>
#include <QTimer>
#include "mainscene.h"
#include "ui_mainscene.h"
#include <gameconfig.h>
#include "gamepushbutton.h"
#include "gamecontroller.h"

Mainscene::Mainscene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Mainscene)
{
    // 初始化界面控制类中mainscene
    GameController::getInstance()->initMainScene(this);
    ui->setupUi(this);
    this->setWindowTitle(tr("翻金币游戏"));
    this->setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    this->setWindowIcon(QIcon(GAME_ICON));
    //创建菜单栏
    QMenuBar* bar = menuBar();
    QMenu* startMenu = bar->addMenu("选项");
    QAction* quitAction = startMenu->addAction("退出");
    QAction* backAction = startMenu->addAction("回退");

    //创建一个槽函数，菜单栏中的退出操作项
    connect(quitAction,&QAction::triggered,[=](){
        close();
    });
    //Start按钮
    GamePushButton* startBtn = new GamePushButton(BTN_START_GAME);
    startBtn->setParent(this);
    startBtn->move((this->width()-startBtn->width())*0.5,this->height()*0.7);

    connect(startBtn,&GamePushButton::clicked,this,[=](){
        startBtn->zoomUp();
        GameController::getInstance()->playStartSound();
        startBtn->zoomDown();
        QTimer::singleShot(500,this,[=](){
            GameController::getInstance()->hideMinaScene();
            GameController::getInstance()->showLevelScene();
        });

    });

}

Mainscene::~Mainscene()
{
    delete ui;
}

void Mainscene::paintEvent(QPaintEvent *event)
{
    QPainter mainPainter(this);
    QPixmap mainPixmap(IMG_BG_MAINSCENE);
    mainPainter.drawPixmap(this->rect(),mainPixmap);
}

